<template>
  <div class="risk">
    <van-search class="info_search" v-model="value" placeholder="搜索" />
    <van-tabs v-model="active">
      <van-tab title="已提交的记录">
        <van-list
          style="margin: 10px"
          :finished="myFinished"
          v-model="myLoading"
          finished-text="没有更多了"
          @load="myOnLoad"
        >
          <van-cell
            style="margin-top: 10px"
            v-for="(item, index) in myList"
            :key="index"
          >
            <div class="warn_item">
              <div @click="goDetail(item)">
                <div class="warn_header">
                  <div>
                    {{ index + 1 }}、隐患描述：{{ item.HiddenDangerDesc }}
                  </div>
                  <van-tag
                    v-if="item.ExpireDays < 0"
                    class="warn_tag"
                    type="warning"
                    >剩{{ Math.abs(item.ExpireDays) }}</van-tag
                  >
                  <van-tag
                    v-if="item.ExpireDays > 0"
                    class="warn_tag"
                    type="warning"
                    >逾期{{ Math.abs(item.ExpireDays) }}</van-tag
                  >
                </div>
                <!-- <div class="dep_sort">
                  <img src="@/assets/Images/AppIcon/icon_list_org.png" alt="" />
                  <div>{{ item.CheckTypeName }}</div>
                </div> -->
                <van-row gutter="20" class="dep_sort">
                  <van-col span="24">
                    <img
                      src="@/assets/Images/AppIcon/icon_list_org.png"
                      alt=""
                    />所属任务：{{ item.CheckTaskName }}</van-col
                  >
                </van-row>

                <van-row gutter="20" class="dep_sort">
                  <van-col span="12">
                    <img
                      src="@/assets/Images/AppIcon/icon_list_post.png"
                      alt=""
                    />{{
                      item.OriginType == 1
                        ? "检查表检查"
                        : item.StatusName == 2
                        ? "依据检查"
                        : "随手拍"
                    }}</van-col
                  >
                </van-row>

                <van-row class="dep_sort">
                  <van-col span="8">
                    <img
                      src="@/assets/Images/AppIcon/icon_list_security.png"
                      alt=""
                    />{{ item.CreateChnName }}</van-col
                  >
                  <van-col span="16">
                    <img
                      src="@/assets/Images/AppIcon/icon_list_security.png"
                      alt=""
                    />{{ item.CreateDate }}</van-col
                  >
                </van-row>
              </div>
            </div>
          </van-cell>
        </van-list>
      </van-tab>
      <van-tab title="未提交的记录">
        <van-list
          style="margin: 10px"
          :finished="finished"
          v-model="loading"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <van-checkbox-group
            ref="checkboxGroup"
            @change="handleChange"
            v-model="result"
          >
            <van-cell
              style="margin-top: 10px"
              v-for="(item, index) in list"
              :key="index"
            >
              <div class="warn_item">
                <div @click="goToDetail(item)">
                  <div class="warn_header">
                    <div :style="{ color: !item.IsCorrect ? 'red' : '' }">
                      {{ index + 1 }}、隐患描述：{{
                        item.HiddenDangerDesc || "暂无任何描述"
                      }}
                    </div>
                    <van-tag
                      v-if="item.ExpireDays < 0"
                      class="warn_tag"
                      type="warning"
                      >剩{{ Math.abs(item.ExpireDays) }}</van-tag
                    >
                    <van-tag
                      v-if="item.ExpireDays > 0"
                      class="warn_tag"
                      type="warning"
                      >逾期{{ Math.abs(item.ExpireDays) }}</van-tag
                    >
                  </div>
                  <!-- <div class="dep_sort">
                  <img src="@/assets/Images/AppIcon/icon_list_org.png" alt="" />
                  <div>{{ item.CheckTypeName }}</div>
                </div> -->
                  <van-row gutter="20" class="dep_sort">
                    <van-col span="24">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_org.png"
                        alt=""
                      />所属任务：{{ item.CheckTaskName }}</van-col
                    >
                  </van-row>

                  <van-row gutter="20" class="dep_sort">
                    <van-col span="12">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_post.png"
                        alt=""
                      />{{
                        item.OriginType == 1
                          ? "检查表检查"
                          : item.StatusName == 2
                          ? "依据检查"
                          : "随手拍"
                      }}</van-col
                    >
                  </van-row>

                  <van-row class="dep_sort">
                    <van-col span="8">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_security.png"
                        alt=""
                      />{{ item.CreateChnName }}</van-col
                    >
                    <van-col span="16">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_security.png"
                        alt=""
                      />{{ item.CreateDate }}</van-col
                    >
                  </van-row>
                </div>
                <template v-if="userName == 1">
                  <van-divider />
                  <div v-if="item.IsCorrect" class="dep_btm">
                    <div class="btm_item" @click="handleAdd(item)">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_edit.png"
                        alt=""
                      />
                      <div class="mr-10">提交检查记录</div>
                    </div>
                  </div>
                </template>
              </div>
            </van-cell>
          </van-checkbox-group>
        </van-list>
        <div class="choose_btm" v-if="userName == 1">
          <div class="choose_cancel">
            <van-button
              class="mr-10"
              type="warning"
              size="small"
              @click="checkAll"
              >全选</van-button
            >
            <van-button
              class="mr-10"
              type="warning"
              @click="toggleAll"
              size="small"
              >取消全选</van-button
            >
            <div>已选择{{ result.length }}个项目</div>
          </div>
          <div class="choose_confirm">
            <van-button type="info" @click="submit">批量提交</van-button>
          </div>
        </div>
      </van-tab>
      <van-tab title="被退回的记录">
        <van-list
          style="margin: 10px"
          :finished="finishFinished"
          v-model="finishLoading"
          finished-text="没有更多了"
          @load="finishOnLoad"
        >
          <van-checkbox-group
            ref="checkboxBackGroup"
            @change="handleBackChange"
            v-model="backResult"
          >
            <van-cell
              style="margin-top: 10px"
              v-for="(item, index) in finishList"
              :key="index"
            >
              <div class="warn_item">
                <div @click="goBackDetail(item)">
                  <div class="warn_header">
                    <div :style="{ color: !item.IsCorrect ? 'red' : '' }">
                      {{ index + 1 }}、隐患描述：{{
                        item.HiddenDangerDesc || "暂无任何描述"
                      }}
                    </div>
                    <van-tag
                      v-if="item.ExpireDays < 0"
                      class="warn_tag"
                      type="warning"
                      >剩{{ Math.abs(item.ExpireDays) }}</van-tag
                    >
                    <van-tag
                      v-if="item.ExpireDays > 0"
                      class="warn_tag"
                      type="warning"
                      >逾期{{ Math.abs(item.ExpireDays) }}</van-tag
                    >
                  </div>
                  <!-- <div class="dep_sort">
                  <img src="@/assets/Images/AppIcon/icon_list_org.png" alt="" />
                  <div>{{ item.CheckTypeName }}</div>
                </div> -->
                  <van-row gutter="20" class="dep_sort">
                    <van-col span="24">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_org.png"
                        alt=""
                      />所属任务：{{ item.CheckTaskName }}</van-col
                    >
                  </van-row>

                  <van-row gutter="20" class="dep_sort">
                    <van-col span="12">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_post.png"
                        alt=""
                      />{{
                        item.OriginType == 1
                          ? "检查表检查"
                          : item.StatusName == 2
                          ? "依据检查"
                          : "随手拍"
                      }}</van-col
                    >
                  </van-row>

                  <van-row class="dep_sort">
                    <van-col span="8">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_security.png"
                        alt=""
                      />{{ item.CreateChnName }}</van-col
                    >
                    <van-col span="16">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_security.png"
                        alt=""
                      />{{ item.CreateDate }}</van-col
                    >
                  </van-row>
                </div>
                <template v-if="userName == 1">
                  <van-divider />
                  <div v-if="item.IsCorrect" class="dep_btm">
                    <div class="btm_item" @click="handleBackAdd(item)">
                      <img
                        src="@/assets/Images/AppIcon/icon_list_edit.png"
                        alt=""
                      />
                      <div class="mr-10">提交检查记录</div>
                    </div>
                  </div>
                </template>
              </div>
            </van-cell>
          </van-checkbox-group>
        </van-list>
        <div class="choose_btm" v-if="userName == 1">
          <div class="choose_cancel">
            <van-button
              class="mr-10"
              type="warning"
              size="small"
              @click="checkBackAll"
              >全选</van-button
            >
            <van-button
              class="mr-10"
              type="warning"
              @click="toggleBackAll"
              size="small"
              >取消全选</van-button
            >
            <div>已选择{{ backResult.length }}个项目</div>
          </div>
          <div class="choose_confirm">
            <van-button type="info" @click="backSubmit">批量提交</van-button>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { list } from "@/api/sytech/sytechDangerCheckTaskDetail";
import { mapGetters } from "vuex";
// import { Toast } from "vant";
import { Dialog } from "vant";
export default {
  data() {
    return {
      active: 0,
      value: "",
      list: [],
      userlist: [],
      backUserList: [],
      myList: [],
      result: [],
      backResult: [],
      finishList: [],
      selectKey: "",
      userName: this.$route.query.userName,
      myFilter: {
        page: 0,
        dataType: "BeSubmited",
        ctCode: this.$route.query.ctCode,
        userName: this.$route.query.userName,
      },
      filter: {
        page: 0,
        dataType: "NotSubmit",
        ctCode: this.$route.query.ctCode,
        userName: this.$route.query.userName,
      },
      finishFilter: {
        page: 0,
        dataType: "BeBacked",
        ctCode: this.$route.query.ctCode,
        userName: this.$route.query.userName,
      },
      loading: false,
      finished: false,
      myLoading: false,
      myFinished: false,
      finishLoading: false,
      finishFinished: false,
    };
  },
  computed: {
    ...mapGetters(["CompanyCode",'token']),
  },
  created() {
    this.getList();
    this.getMyList();
    this.getFinishList();
  },
  methods: {
    checkAll() {
      this.$refs.checkboxGroup.toggleAll(true);
    },
    toggleAll() {
      this.$refs.checkboxGroup.toggleAll();
    },
    checkBackAll() {
      this.$refs.checkboxBackGroup.toggleAll(true);
    },
    toggleBackAll() {
      this.$refs.checkboxBackGroup.toggleAll();
    },
    getMyList() {
      this.myLoading = true;
      list(this.myFilter).then((res) => {
        this.myLoading = false;
        if (!res.data.HasNextPage) {
          this.myFinished = true;
        }
        this.myList = res.data.Data;
        this.myFilter.page = res.data.PageIndex;
      });
    },
    getList() {
      this.loading = true;
      list(this.filter).then((res) => {
        this.loading = false;
        if (!res.data.HasNextPage) {
          this.finished = true;
        }
        this.list = res.data.Data;
        this.filter.page = res.data.PageIndex;
      });
    },
    getFinishList() {
      this.finishLoading = true;
      list(this.finishFilter).then((res) => {
        this.finishLoading = false;
        if (!res.data.HasNextPage) {
          this.finishFinished = true;
        }
        this.finishList = res.data.Data;
        this.finishFilter.page = res.data.PageIndex;
      });
    },

    handleChange(ids) {
      this.userlist = [];
      ids.map((item) => {
        this.list.map((innerItem, innerIndex) => {
          if (innerItem.ID == item) {
            this.userlist.push({
              ID: innerItem.ID,
              IsCorrect: innerItem.IsCorrect,
              innerIndex: innerIndex + 1,
            });
          }
        });
      });
    },

    handleBackChange(ids) {
      this.backUserList = [];
      ids.map((item) => {
        this.finishList.map((innerItem, innerIndex) => {
          if (innerItem.ID == item) {
            this.backUserList.push({
              ID: innerItem.ID,
              IsCorrect: innerItem.IsCorrect,
              innerIndex: innerIndex + 1,
            });
          }
        });
      });
    },

    submit() {
      for (let i = 0; i < this.userlist.length; i++) {
        if (!this.userlist[i].IsCorrect) {
          this.$toast(
            "第" +
              this.userlist[i].innerIndex +
              "项信息未填写完整，请先填写完后再选择"
          );
          return;
        }
      }

      let data = [];
      this.userlist.map((item, index) => {
        data.push(item.ID);
      });
      SubmitRecords(data)
        .then((res) => {
          this.$toast.success("提交成功!");
          this.getList();
          this.getMyList();
          this.getFinishList();
        })
        .catch((err) => {
          console.log("err", err);
        });
      // this.$router.push({
      //   name: "edit-chemical",
      //   params: {userlist:this.userlist},
      // });
    },

    backSubmit() {
      for (let i = 0; i < this.backUserList.length; i++) {
        if (!this.backUserList[i].IsCorrect) {
          let index = i + 1;
          this.$toast(
            "第" +
              this.backUserList[i].innerIndex +
              "项信息未填写完整，请先填写完后再选择"
          );
          return;
        }
      }
      let data = [];
      this.backUserList.map((item, index) => {
        data.push(item.ID);
      });
      SubmitRecords(data)
        .then((res) => {
          this.$toast.success("提交成功!");
          this.getList();
          this.getMyList();
          this.getFinishList();
        })
        .catch((err) => {
          console.log("err", err);
        });
    },

    handleAdd(item) {
      Dialog.confirm({
        title: "询问",
        message: "你确定要提交该检查记录吗?",
      })
        .then(() => {
          // on confirm
          handleAdd({ id: item.ID }).then((res) => {
            this.$toast.success("提交成功！");
            this.getList();
            // console.log('asdasdsad',res);
          });
        })
        .catch(() => {
          // on cancel
        });
    },

    handleBackAdd(item) {
      Dialog.confirm({
        title: "询问",
        message: "你确定要提交该检查记录吗?",
      })
        .then(() => {
          // on confirm
          handleAdd({ id: item.ID }).then((res) => {
            this.$toast.success("提交成功！");
            this.getList();
            // console.log('asdasdsad',res);
          });
        })
        .catch(() => {
          // on cancel
        });
    },

    myOnLoad() {
      this.myLoading = true;
      ++this.myFilter.page;
      list(this.myFilter).then((res) => {
        this.myLoading = false;
        this.myFilter.page = res.data.PageIndex;
        this.myList = this.myList.concat(res.data.Data);
        if (!res.data.HasNextPage) {
          this.myFinished = true;
        }
      });
    },

    finishOnLoad() {
      this.finishLoading = true;
      ++this.finishFilter.page;
      list(this.finishFilter).then((res) => {
        this.finishLoading = false;
        this.finishFilter.page = res.data.PageIndex;
        this.finishList = this.finishList.concat(res.data.Data);
        if (!res.data.HasNextPage) {
          this.finishFinished = true;
        }
      });
    },
    onLoad() {
      this.loading = true;
      ++this.filter.page;
      list(this.filter).then((res) => {
        this.loading = false;
        this.filter.page = res.data.PageIndex;
        this.list = this.list.concat(res.data.Data);
        if (!res.data.HasNextPage) {
          this.finished = true;
        }
      });
    },

    goDetail(item) {
      this.$router.push({
        path: "/SytechDangerCheckDetailView",
        query: { id: item.ID },
      });
    },
    // 随手拍编辑
    goToDetail(item) {
      this.$router.push({
        path: "DangerCheckDetailView",
        query: { id: item.ID },
      });
    },
    // 检查表检查编辑
    goBackDetail(item) {
      this.$router.push({
        path: "DangerCheckDetailView",
        query: { id: item.ID, type: true },
      });
    },
  },
};
</script>

<style lang="less">
.risk {
  background-color: #e8f1f8;
  .info_search {
    padding: 10px;
    border-radius: 5px !important;
    .van-field__control {
      background-color: #ffffff !important;
    }
  }
}
.all_dep {
  background-color: #ffffff;
}
.warn_header {
  display: flex;
  justify-content: space-between;
  position: relative;
  margin-bottom: 10px;
  .warn_tag {
    position: absolute;
    right: 0;
    top: 0;
    border-radius: 26.64rem 0 0 26.64rem;
  }
}
.dep_sort {
  // display: flex;
  // flex-wrap: wrap;
  // margin-bottom: 5px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.dep_btm {
  display: flex;
  justify-content: flex-end;
  .btm_item {
    display: flex;
    align-items: center;
    img {
      height: 15px;
      width: 15px;
      margin-right: 5px;
    }
  }
}

.choose_btm {
  display: flex;
  width: 100%;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
  background-color: #fff;
  .choose_cancel {
    padding: 0 10px;
    display: flex;
    align-items: center;
    float: left;
  }
  .choose_confirm {
    float: right;
    border-radius: 0;
  }
}
</style>
